package cn.xfdzx.android.apps.shop.activity.myshop;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.activity.ruzhu.XFDShopInActivity;
import cn.xfdzx.android.apps.shop.adapter.GridImageAdapter;
import cn.xfdzx.android.apps.shop.adapter.SelectOnePwAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.ImageUrlBeen;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodTypeBeen;
import cn.xfdzx.android.apps.shop.bean.ShopGoodsInfoBeen;
import cn.xfdzx.android.apps.shop.bean.UploadImgBeen;
import cn.xfdzx.android.apps.shop.contract.ShopAddGoodContract;
import cn.xfdzx.android.apps.shop.presenter.ShopAddGoodPresenter;
import cn.xfdzx.android.apps.shop.util.GlideEngine;
import cn.xfdzx.android.apps.shop.util.ItemOffsetDecoration;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.ShopCarPopupWindow;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.util.UtilsLog;
import cn.xfdzx.android.apps.shop.widget.WheelPicker;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

public class ShopAddGoodsActivity extends BaseMvpActivity<ShopAddGoodPresenter> implements ShopAddGoodContract.View {

    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.ett_good_name)
    EditText ettGoodName;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.ett_gross_weight)
    EditText ettGrossWeight;//毛重
    @BindView(R.id.ett_price)
    EditText ettPrice;//价格
    @BindView(R.id.ett_preferential)
    EditText ettPreferential;//优惠价格
    @BindView(R.id.ett_inventory)
    EditText ettInventory;//
    @BindView(R.id.tv_one_item)
    TextView tvOneItem;//
    @BindView(R.id.tv_two_item)
    TextView tvTwoItem;//
    @BindView(R.id.tv_next)
    TextView tvNext;//
    @BindView(R.id.tv_next_shangjia)
    TextView tvNextShangjia;//
    @BindView(R.id.rv_image)
    RecyclerView rvImage;//
    @BindView(R.id.tv_shop_type)
    TextView tvShopType;//
    @BindView(R.id.rl_shop_type)
    RelativeLayout rlShopType;//
    @BindView(R.id.tv_good_weight)
    TextView tvGoodWeight;
    private RelativeLayout.LayoutParams item_bar;
    protected Handler handler = new Handler();
    private Context mContext;

    private ShopCarPopupWindow unitPopupWindow;
    private WheelPicker wheelPicker;

    private int one_position = -1;//已选一级
    private int two_position = -1;//已选二级

    private List<String> selectList = new ArrayList<>();
    private GridImageAdapter adapter;
    private List<String> unitList = new ArrayList<>();
    private String type = "";
    private String good_id;
    private List<CategoryAllBeen.DataBean> typeData;//一二级分类
    private boolean is_select_image = false;//是否是图库点击

    private ShopAddGoodTypeBeen shopAddGoodTypeBeen;//店铺分类集合
    private ShopGoodsInfoBeen shopGoodsInfoBeen;//修改商品的商品详情
    private boolean isUnit = true;//是否是商品单位
    private String store_category_id = "1";//店铺分类id
    private String state = "";//上架状态0-未上架 1-已上架
    private Dialog dialog;

    private int unit_position = -1;//记录单位位置
    private int type_position = -1;//记录商品分类位置

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_add_goods;
    }

    @Override
    public void initView() {
        initHigh();
        StatusBarUtil.setLightStatusBar(this, true, true);
        mContext = ShopAddGoodsActivity.this;
        mPresenter = new ShopAddGoodPresenter();
        mPresenter.attachView(this);

        initWidget();
        type = getIntent().getStringExtra("type");
        good_id = getIntent().getStringExtra("good_id");
        if (type.equals("0")) {
            rlShopType.setVisibility(View.VISIBLE);
            mPresenter.initTypeData("1", 0);
            tvNext.setBackgroundResource(R.drawable.d999999_bg_r8);
            tvNextShangjia.setBackgroundResource(R.drawable.dfb9129_bg_r8);
            tvGoodWeight.setText("商品重量(克)");
        } else {
            rlShopType.setVisibility(View.GONE);
            mPresenter.initTypeData("2", 0);
            tvNext.setBackgroundResource(R.drawable.d999999_bg_r8);
            tvNextShangjia.setBackgroundResource(R.drawable.d02c557_bg_r8);
        }
        //修改商品
        if (null != good_id) {
            mPresenter.goodsInfo(good_id);
            tvTitle.setText("修改商品");
            tvNext.setText("修改商品但不上架");
            tvNextShangjia.setText("修改商品立即上架");
        }
        mPresenter.shopAddGoodType(false);
        popupWindow();
    }


    private void popupWindow() {
        unitPopupWindow = new ShopCarPopupWindow(this, R.layout.pw_unit);
        wheelPicker = unitPopupWindow.getRootView().findViewById(R.id.up_unit);


        unitPopupWindow.getRootView().findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unitPopupWindow.shareClose();
            }
        });
        if (type.equals("0")) {
            ((TextView) unitPopupWindow.getRootView().findViewById(R.id.tv_queren)).setTextColor(getResources().getColor(R.color.color_FB9129));
        } else {
            ((TextView) unitPopupWindow.getRootView().findViewById(R.id.tv_queren)).setTextColor(getResources().getColor(R.color.color_02C557));
        }
        unitPopupWindow.getRootView().findViewById(R.id.tv_queren).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isUnit) {
                    unit_position = wheelPicker.getCurrentPosition();
                    tvUnit.setText(unitList.get(unit_position));
                } else {
                    type_position = wheelPicker.getCurrentPosition();
                    tvShopType.setText(unitList.get(type_position));
                    store_category_id = shopAddGoodTypeBeen.getData().get(type_position).getId() + "";
                }
                unitPopupWindow.shareClose();
            }
        });

    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }


    private void initWidget() {
        rvImage.setItemAnimator(new DefaultItemAnimator());
        GridLayoutManager mLayoutManager = new GridLayoutManager(this, 4);
        rvImage.setLayoutManager(mLayoutManager);
        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(this, R.dimen.item_offset);
        rvImage.addItemDecoration(itemDecoration);
        rvImage.setNestedScrollingEnabled(false);
        adapter = new GridImageAdapter(this);
        adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ImageView imageView) {
                selectImage(5 - selectList.size());
            }
        });
        adapter.setList(selectList);
        adapter.setSelectMax(5);
        rvImage.setAdapter(adapter);

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onAddGoodSuccess(ShopAddGoodBeen bean) {
        Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent(this, GoodManagementActivity.class);
            intent.putExtra("type", type);
            intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();
        }
    }

    @Override//修改商品
    public void onUpdataSuccess(ShopAddGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent();
            setResult(RESULT_OK,intent);
            finish();
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onShopAddGoodTypeSuccess(ShopAddGoodTypeBeen bean, boolean is_visibility) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            this.shopAddGoodTypeBeen = bean;
            //修改商品

            if (is_visibility) {
                unitList.clear();
                for (ShopAddGoodTypeBeen.DataBean dataBean : bean.getData()) {
                    unitList.add(dataBean.getName());
                }
                wheelPicker.setDataList(unitList);
                unitPopupWindow.showPopupWindow();
            }

            if (null != good_id) {
                if (null != shopGoodsInfoBeen) {
                    for (int j = 0; j < shopAddGoodTypeBeen.getData().size(); j++) {
                        if (shopAddGoodTypeBeen.getData().get(j).getId() == shopGoodsInfoBeen.getData().getStore_category_id()) {
                            store_category_id = shopAddGoodTypeBeen.getData().get(j).getId() + "";
                            type_position = j;
                        }
                    }
                }
                tvShopType.setText(shopGoodsInfoBeen.getData().getStore_category_name());
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override///修改商品 商品详情
    public void onGoodsInfoSuccess(ShopGoodsInfoBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            shopGoodsInfoBeen = bean;
            if (null != typeData) {
                for (int k = 0; k < typeData.size(); k++) {
                    if (Integer.parseInt(typeData.get(k).getId()) == shopGoodsInfoBeen.getData().getCategory_pid()) {
                        one_position = k;
                        for (int y = 0; y < typeData.get(one_position).getChild().size(); y++) {
                            if ((typeData.get(one_position).getChild().get(y).getId() + "").equals(shopGoodsInfoBeen.getData().getCategory_id())) {
                                two_position = y;
                            }
                        }
                    }
                }
                tvType.setText(typeData.get(one_position).getName() + "<" + typeData.get(one_position).getChild().get(two_position).getName());
            }
            if (null != shopAddGoodTypeBeen) {
                for (int j = 0; j < shopAddGoodTypeBeen.getData().size(); j++) {
                    if (shopAddGoodTypeBeen.getData().get(j).getId() == shopGoodsInfoBeen.getData().getStore_category_id()) {
                        store_category_id = shopAddGoodTypeBeen.getData().get(j).getId() + "";
                        type_position = j;
                    }
                }

            }
            selectList.addAll(bean.getData().getThumbs());
            adapter.notifyDataSetChanged();
            ettGoodName.setText(bean.getData().getTitle());
            ettGrossWeight.setText(bean.getData().getGoods_weight());
            ettPrice.setText(bean.getData().getMarketprice());
            ettPreferential.setText(bean.getData().getDiscountprice());
            ettInventory.setText(bean.getData().getStock() + "");
            tvUnit.setText(bean.getData().getUnit() + "");
            if (null!=good_id) {
                if (bean.getData().getUnit().equals("件")) {
                    unit_position = 0;
                } else if (bean.getData().getUnit().equals("箱")) {
                    unit_position = 1;
                } else if (bean.getData().getUnit().equals("袋")) {
                    unit_position = 2;
                }
                tvShopType.setText(shopGoodsInfoBeen.getData().getStore_category_name());
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    private boolean is_frist = false;

    @Override//一二级分类
    public void onInitTypeSuccess(CategoryAllBeen bean, int is_tiao) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            typeData = bean.getData();
            if (null != good_id) {
                if (null != shopGoodsInfoBeen) {
                    for (int k = 0; k < typeData.size(); k++) {
                        if (Integer.parseInt(typeData.get(k).getId()) == shopGoodsInfoBeen.getData().getCategory_pid()) {
                            one_position = k;
                            for (int y = 0; y < typeData.get(one_position).getChild().size(); y++) {
                                if ((typeData.get(one_position).getChild().get(y).getId() + "").equals(shopGoodsInfoBeen.getData().getCategory_id())) {
                                    two_position = y;
                                }
                            }
                        }
                    }
                    tvType.setText(typeData.get(one_position).getName() + "<" + typeData.get(one_position).getChild().get(two_position).getName());
                }
            }
            if (is_tiao == 1) {
                if (is_frist) {
                    intent = new Intent(this, SelectImageActivity.class);
                    intent.putExtra("data", (Serializable) typeData);
                    intent.putExtra("one_position", one_position);
                    intent.putExtra("two_position", two_position);
                    intent.putExtra("tuku", is_select_image);
                    intent.putExtra("type", type);
                    startActivityForResult(intent, START_CODE);
                }
                is_frist = true;
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    Intent intent;

    @OnClick({R.id.iv_back, R.id.tv_next, R.id.tv_next_shangjia, R.id.tv_unit, R.id.tv_type, R.id.ll_select_image, R.id.tv_shop_type})
    public void Onclick(View view) {
        switch (view.getId()) {

            case R.id.iv_back:
                finish();
                break;

            case R.id.tv_unit:
                isUnit = true;
                unitList.clear();
                unitList.add("件");
                unitList.add("箱");
                unitList.add("袋");
                wheelPicker.setDataList(unitList);

                if (unit_position != -1) {
                    wheelPicker.setCurrentPosition(unit_position);
                }
                unitPopupWindow.showPopupWindow();
                break;
            case R.id.tv_shop_type:
                isUnit = false;
                unitList.clear();
                if (null != shopAddGoodTypeBeen) {
                    for (ShopAddGoodTypeBeen.DataBean dataBean : shopAddGoodTypeBeen.getData()) {
                        unitList.add(dataBean.getName());
                    }
                    wheelPicker.setDataList(unitList);

                    if (type_position != -1) {
                        wheelPicker.setCurrentPosition(type_position);
                    }
                    unitPopupWindow.showPopupWindow();
                } else {
                    mPresenter.shopAddGoodType(true);
                }
                break;
            case R.id.tv_type:
                is_select_image = false;
                if (null != typeData) {
                    intent = new Intent(this, SelectImageActivity.class);
                    intent.putExtra("data", (Serializable) typeData);
                    intent.putExtra("one_position", one_position);
                    intent.putExtra("two_position", two_position);
                    intent.putExtra("tuku", is_select_image);
                    intent.putExtra("type", type);
                    startActivityForResult(intent, START_CODE);
                } else {
                    if (type.equals("0")) {
                        mPresenter.initTypeData("1", 1);
                    } else {
                        mPresenter.initTypeData("2", 1);
                    }
                }
                break;
            case R.id.ll_select_image:
                is_select_image = true;
                if (null != typeData) {
                    intent = new Intent(this, SelectImageActivity.class);
                    intent.putExtra("data", (Serializable) typeData);
                    intent.putExtra("one_position", one_position);
                    intent.putExtra("two_position", two_position);
                    intent.putExtra("tuku", is_select_image);
                    intent.putExtra("type", type);
                    startActivityForResult(intent, START_CODE);
                } else {
                    if (type.equals("0")) {
                        mPresenter.initTypeData("1", 1);
                    } else {
                        mPresenter.initTypeData("2", 1);
                    }
                }
                break;
            case R.id.tv_next:
                state = "0";
                public_addGood(false);

                break;
            case R.id.tv_next_shangjia:
                state = "1";
                public_addGood(true);
                break;
        }

    }

    private void public_addGood(boolean shangjia) {
        int hint;
        if (null == good_id) {
            if (shangjia) {
                hint = R.string.add_good_immediately;
            } else {
                hint = R.string.add_good_but_not;
            }
        } else {
            if (shangjia) {
                hint = R.string.update_good_immediately;
            } else {
                hint = R.string.update_good_but_not;
            }
        }

        if (tvType.getText().toString().trim().equals("请选择")) {
            Toast.makeText(mContext, "选择分类", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ettGoodName.getText().toString().trim().isEmpty()) {
            Toast.makeText(mContext, "请输入商品名称", Toast.LENGTH_SHORT).show();
            return;
        }
        if (selectList.size() == 0) {
            Toast.makeText(mContext, "请选择商品图片", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ettGrossWeight.getText().toString().trim().isEmpty()) {
            Toast.makeText(mContext, "请输入商品毛重", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ettPrice.getText().toString().trim().isEmpty()) {
            Toast.makeText(mContext, "请输入商品原价", Toast.LENGTH_SHORT).show();
            return;
        }
        if (ettInventory.getText().toString().trim().isEmpty()) {
            Toast.makeText(mContext, "请输入库存数量", Toast.LENGTH_SHORT).show();
            return;
        }
        if (tvUnit.getText().toString().trim().equals("请选择")) {
            Toast.makeText(mContext, "请选择商品单位", Toast.LENGTH_SHORT).show();
            return;
        }
        if (type.equals("0")) {
            if (tvShopType.getText().toString().trim().equals("请选择")) {
                Toast.makeText(mContext, "请选择店铺分类", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        dialog = Utils.addDialog(this, R.layout.dialog, hint, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                post_request();
            }
        });
        dialog.show();
    }

    public void post_request() {
        Gson gson = new Gson();
        List<ImageUrlBeen> mList = new ArrayList<>();
        for (int k = 0; k < selectList.size(); k++) {
            ImageUrlBeen imageUrlBeen = new ImageUrlBeen();
            imageUrlBeen.url = selectList.get(k);
            mList.add(imageUrlBeen);
        }
        if (null == good_id) {
            mPresenter.addGood(state, ettGoodName.getText().toString().trim(), gson.toJson(mList), ettPrice.getText().toString().trim(), ettPreferential.getText().toString().trim(), ettInventory.getText().toString().trim(), typeData.get(one_position).getChild().get(two_position).getId() + "", store_category_id + "", ettGrossWeight.getText().toString().trim(), tvUnit.getText().toString().trim());
        } else {
            mPresenter.updataGood(good_id, state, ettGoodName.getText().toString().trim(), gson.toJson(mList), ettPrice.getText().toString().trim(), ettPreferential.getText().toString().trim(), ettInventory.getText().toString().trim(), typeData.get(one_position).getChild().get(two_position).getId() + "", store_category_id + "", ettGrossWeight.getText().toString().trim(), tvUnit.getText().toString().trim());
        }
    }

    public void selectImage(int max_size) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage()).maxSelectNum(max_size)
                .loadImageEngine(GlideEngine.createGlideEngine())
                .minSelectNum(1).imageSpanCount(4)
                .selectionMode(PictureConfig.MULTIPLE)
                .compress(true)
                .minimumCompressSize(1000)
                .compressQuality(60)
                .isCamera(true)
                .previewImage(true)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        // onResult Callback
                        if (result.size() != 0) {
                            UtilsLog.error("dizhi", result.get(0).getCompressPath());
//                                    new File(result.get(0).getPath())
//                                    mPresenter.upload(new File(result.get(0).getPath()));
                            showLoading();
                            for (int k = 0; k < result.size(); k++) {
                                upLoadFiles(Constants.BASE_URL + "/addons/shimmer_liveshop/index.php/api/6/3/plugin/merch/common/apply/uploadImg", new File(result.get(k).getCompressPath()), new XFDShopInActivity.ConnectListener() {
                                    @Override
                                    public void connectSuccess(String result) {
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                hideLoading();
                                                try {
                                                    Gson gson = new Gson();
                                                    UploadImgBeen uploadImgBeen = gson.fromJson(result, UploadImgBeen.class);
                                                    if (uploadImgBeen.getErrno().equals("200")) {
                                                        selectList.add(uploadImgBeen.getData().getSrc());
                                                        adapter.notifyDataSetChanged();
                                                    } else {
                                                        Toast.makeText(mContext, uploadImgBeen.getMessage(), Toast.LENGTH_SHORT).show();
                                                    }
                                                } catch (Exception e) {
                                                    Toast.makeText(mContext, "上传失败", Toast.LENGTH_SHORT).show();

                                                }
                                            }
                                        });

                                    }

                                    @Override
                                    public void connectFailed(String error) {
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                hideLoading();

                                            }
                                        });
                                    }
                                });
                            }
                        }
                    }

                    @Override
                    public void onCancel() {
                        // onCancel Callback
                    }
                });
    }


    public void upLoadFiles(final String url, File file, final XFDShopInActivity.ConnectListener connectListener) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(Proxy.NO_PROXY).connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .writeTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .build();
        MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
        multipartBodyBuilder.setType(MultipartBody.FORM);
        //遍历map中所有参数到builder
        multipartBodyBuilder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
        //构建请求体
        RequestBody requestBody = multipartBodyBuilder.build();
        Request.Builder RequestBuilder = new Request.Builder();
        RequestBuilder.url(url);// 添加URL地址
        RequestBuilder.post(requestBody);
        RequestBuilder.addHeader("Authori-zation", "Bearer " + APP_TOKEN);
        Request request = RequestBuilder.build();

        final Call call = okHttpClient.newCall(request);
        //异步get请求
        call.enqueue(new Callback() {
                         @Override
                         public void onFailure(Call call, IOException e) {
                             connectListener.connectFailed(e.toString());
                             UtilsLog.error("错误", e.toString());
                         }

                         @Override
                         public void onResponse(Call call, Response response) throws IOException {
                             String result = response.body().string();
                             connectListener.connectSuccess(result);
                             UtilsLog.error("成功", result);
                         }
                     }
        );
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == START_CODE) {
                one_position = data.getIntExtra("one_position", -1);
                two_position = data.getIntExtra("two_position", -1);
                List<String> tukuImageList = data.getStringArrayListExtra("stringList");
                if (tukuImageList != null) {
                    if (data.getStringExtra("goodName").equals(typeData.get(one_position).getChild().get(two_position).getName())){
                        ettGoodName.setText(data.getStringExtra("goodName"));
                    }else {
                        ettGoodName.setText(data.getStringExtra("goodName") + typeData.get(one_position).getChild().get(two_position).getName());
                    }
                    selectList.clear();
                    selectList.addAll(tukuImageList);
                    adapter.notifyDataSetChanged();
                }
                if (one_position != -1) {
                    tvType.setText(typeData.get(one_position).getName() + "<" + typeData.get(one_position).getChild().get(two_position).getName());
                }
            }
        }
    }
}
